package com.example.mahao

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
import java.io.BufferedReader
import java.io.FileInputStream
import java.io.InputStreamReader

class SecondActivity : AppCompatActivity() {

    private lateinit var listView: ListView
    private lateinit var primeList: MutableList<String>

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        listView = findViewById(R.id.listView)
        primeList = mutableListOf()

        // 从文件中读取素数
        loadPrimesFromFile()

        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, primeList)
        listView.adapter = adapter
    }

    private fun loadPrimesFromFile() {
        try {
            val fileInputStream: FileInputStream = openFileInput("prime_results.txt")
            val reader = BufferedReader(InputStreamReader(fileInputStream))
            var line: String?

            while (reader.readLine().also { line = it } != null) {
                primeList.add(line ?: "")
            }

            reader.close() // 关闭文件流
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}
